package com.cms.recursoshumanos.utilities;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.cms.recursoshumanos.component.interfaces.URLDataCallback;

import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;

public class HttpIimageDataExtrator 
{
    static String url_="";
    static StringBuffer rawResponse=null;
    //static String result = null;
         public static void getWebData(String url, final URLDataCallback callback) throws IOException 
         {
             //url_=url;

                 HttpConnection connection = null;  
                 InputStream inputStream = null;  

                try 
                {  


                    if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                            && RadioInfo
                                    .areWAFsSupported(RadioInfo.WAF_WLAN)) 
                    {
                        url += ";interface=wifi";
                    }

                    connection = (HttpConnection) Connector.open(url, Connector.READ, true);  

                    String location=connection.getHeaderField("location");

                    if(location!=null)
                    {


                        if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                                && RadioInfo
                                        .areWAFsSupported(RadioInfo.WAF_WLAN)) 
                        {
                            location += ";interface=wifi";
                        }


                        connection = (HttpConnection) Connector.open(location, Connector.READ, true);  

                    }
                    else
                    {
                        connection = (HttpConnection) Connector.open(url, Connector.READ, true);  
                    }


                    inputStream = connection.openInputStream();  
                    byte[] responseData = new byte[10000];  
                    int length = 0;  
                    rawResponse = new StringBuffer();  
                    while (-1 != (length = inputStream.read(responseData))) 
                    { 
                        rawResponse.append(new String(responseData, 0, length));  
                    }  
                    int responseCode = connection.getResponseCode();  
                    if (responseCode != HttpConnection.HTTP_OK)
                    {
                        throw new IOException("HTTP response code: "+ responseCode);  
                    }  

                    final String  result = rawResponse.toString();
                    UiApplication.getUiApplication().invokeLater(new Runnable() 
                    {  
                        public void run()
                        {  
                            callback.callback(result);  
                        }  
                    });  
                }  
                catch (final Exception ex) {  
                    UiApplication.getUiApplication().invokeLater(new Runnable() 
                    {  
                        public void run() 
                        {  
                            callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());  
                        }  
                    });  
                }
    }  

}
